const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        address: {},
        id: 0,
        goods: {},
        num: 1
    },
    my_address: function () {
        wx.navigateTo({
            url: '/pages/add_address/add_address',
        })
    },
    exchange_success: function () {
        app.ajax({
            url: '/api/user/exchange',
            data: {
                id: this.data.id,
                num: this.data.num
            },
            success: res => {
                if(res.code == 1){
                    wx.navigateTo({
                        url: '/pages/exchange_success/exchange_success',
                    })
                }else{
                    wx.showToast({
                      title: res.msg,
                      icon: 'none'
                    })
                }
            }
        }, 1);
    },
    add: function(){
        var num = this.data.num;
        num++;
        this.setData({
            num: num
        })
    },
    sub: function(){
        var num = this.data.num;
        num--;
        if(num > 0)
        this.setData({
            num: num
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var id = options.id || 0;
        this.setData({
            id: id
        })
        this.getGoods();
    },

    getGoods: function(){
        app.ajax({
            url: '/api/index/goodsInfo',
            data: {
                id: this.data.id
            },
            success: res => {
                if(res.code == 1){
                    this.setData({
                        goods: res.data
                    })
                }else{
                    wx.showToast({
                      title: res.msg,
                      icon: 'none'
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    getAddress: function(){
        app.ajax({
            url: '/api/user/getAddress',
            success: res => {
                if(res.code == 1){
                    this.setData({
                        address: res.data
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getAddress();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})